﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using User_Interface_Design.DTO;

namespace User_Interface_Design.DAO
{
    class DataAccess
    {
        //public List<Phim> LayDSPhim(int theloai)
        //{
        //    List<Phim> kq = new List<Phim>();
        //    databaseDataContext CHvideo = new databaseDataContext();
        //    kq = (from p in CHvideo.Phims  where p.LoaiPhim == theloai select p).ToList();
        //    //double dongia = (from lp in CHvideo.LoaiPhims where lp.MaLoaiPhim == theloai select lp.DonGia).First();
        //    return kq;
        //}
        //public DTO.Phim LayTTPhim(List<Phim> dsp, string ten, double dongia, int theloai, DateTime ngayphathanh)
        //{
        //    databaseDataContext CHvideo = new databaseDataContext();
        //    DTO.Phim phim = new DTO.Phim();
        //    Phim ph=new Phim();
        //    // chac dat if trong day cho chắc
        //    if (ten != null)
        //    {
        //        if (dongia != 0)
        //        {
        //            if (theloai != 0)
        //            {
        //                if (ngayphathanh != null)
        //                {
        //                    ph = (from p in dsp where p.TenPhim.Contains(ten)  && p.TriGia == dongia && p.LoaiPhim == theloai && p.NgayPhatHanh == ngayphathanh select p).FirstOrDefault();
        //                }
        //                else
        //                    ph = (from p in dsp where p.TenPhim.Contains(ten) && p.TriGia == dongia && p.LoaiPhim == theloai select p).FirstOrDefault();
        //            }
        //            else ph = (from p in dsp where p.TenPhim.Contains(ten) && p.TriGia == dongia select p).FirstOrDefault();
        //        }
        //        else ph = (from p in dsp where p.TenPhim.Contains(ten) select p).FirstOrDefault();

        //    }
        //    else {
        //        if (dongia != 0)
        //        {
        //            if (theloai != 0)
        //            {
        //                if (ngayphathanh != null)
        //                {
        //                    ph = (from p in dsp where p.TriGia == dongia && p.LoaiPhim == theloai && p.NgayPhatHanh == ngayphathanh select p).FirstOrDefault();
        //                }
        //                else
        //                    ph = (from p in dsp where p.TriGia == dongia && p.LoaiPhim == theloai select p).FirstOrDefault();
        //            }
        //            else ph = (from p in dsp where p.TriGia == dongia select p).FirstOrDefault();
        //        }
        //        else ph = null;
        //    }
           
        //    //  
        //    if (ph != null)
        //    {
        //        phim.ten = ph.TenPhim;
        //        phim.ngay = ph.NgayPhatHanh.Value;
        //        phim.soluong = ph.SoLuong.Value;
        //        phim.conlai = ph.ConLai.Value;
        //        phim.dongia = dongia;
        //        phim.theloai = theloai;
        //    }
        //    return phim;
        //}

        public static List<KhachHang> TimKiemKhachHang(String p_ten_CMND, String p_gioitinh, String p_NgaySinh, String p_DiaChi, String p_dienthoai)
        { 
            databaseDataContext d = new databaseDataContext();
            List<KhachHang> kq = new List<KhachHang>();
            kq = (from p in d.KhachHangs where ( p.CMND == p_ten_CMND || p.HoTen.Contains(p_ten_CMND) )select p).ToList();

            List<KhachHang> indexToRemove = new List<KhachHang>();

            foreach (KhachHang kh in kq)
            {
                if (p_gioitinh != "Tất cả")
                {
                    if (!kh.GioiTinh.Contains(p_gioitinh))
                        indexToRemove.Add(kh);
                }
                else if (p_NgaySinh != "")
                {
                    if (kh.NgaySinh != DateTime.Parse( p_NgaySinh) )
                        indexToRemove.Add(kh);
                }
                else if (p_DiaChi != "")
                {
                    if (!kh.DiaChi.Contains(p_DiaChi))
                        indexToRemove.Add(kh);
                } else if (p_dienthoai != "")
                {
                    if (!kh.DienThoai.Contains(p_dienthoai))
                        indexToRemove.Add(kh);
                }
            }

            foreach (KhachHang kh in indexToRemove)
                kq.Remove(kh);
            return kq;
        }

        public static List<KhachHang> TimKiemKhachHang(String p_ten_CMND)
        {
            databaseDataContext d = new databaseDataContext();
            List<KhachHang> kq = new List<KhachHang>();
            kq = (from p in d.KhachHangs where (p.CMND == p_ten_CMND || p.HoTen.Contains(p_ten_CMND)) select p).ToList();

            return kq;
        }
    }
}
